package com.example.simin_manage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.simin_manage.admim.dto.UserWithDept;
import com.example.simin_manage.entity.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @description:
 * @author: panpan
 * @time: 2025/9/14
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
    @Select("SELECT u.id, u.username, u.nickname, u.dept_id, u.role, u.status, d.name AS deptName " +
            "FROM sys_user u " +
            "LEFT JOIN sys_dept d ON u.dept_id = d.id")
        // 普通查询所有用户
    List<UserWithDept> selectAllUsersWithDept();



    // 模糊查询
    // 模糊查询
    @Select("<script>" +
            "SELECT u.id, u.username, u.nickname, u.dept_id, u.role, u.status, d.name AS deptName " +
            "FROM sys_user u " +
            "LEFT JOIN sys_dept d ON u.dept_id = d.id " +
            "WHERE 1=1 " +
            "<if test='keyword != null and keyword != \"\"'>AND (u.username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR u.nickname LIKE CONCAT('%', #{keyword}, '%'))</if> " +
            "<if test='deptId != null and deptId != \"\"'>AND u.dept_id = #{deptId}</if> " +
            "<if test='role != null and role != \"\"'>AND u.role = #{role}</if> " +
            "<if test='status != null'>AND u.status = #{status}</if> " +
            "</script>")
    List<UserWithDept> selectUsersByFilters(
            @Param("keyword") String keyword,
            @Param("role") String role,
            @Param("status") Integer status,
            @Param("deptId") String deptId
    );


}
